package com.bigfreakindeals.Controller;



import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipInputStream;

import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import com.bigfreakindeals.R;
import com.bigfreakindeals.R.drawable;
import com.bigfreakindeals.Application.ApplicationEx;
import com.bigfreakindeals.Common.Constant;

import android.R.integer;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.util.Log;
import android.webkit.WebView;
import android.widget.ProgressBar;

public class ZipDownloader extends AsyncTask<String[], Integer, HashMap<String, Bitmap>>
{
        public File fl = null;
        public Context context = null;
        public int totalUpdates;
        WebView webView = null;
        long len; 
        String slug ;//= "AZSTU.";
        public String BEANSAYS_BTN_PUSH ;
		public String BEANSAYS_BTN_REST;// =  slug+".beans.says.rest.small.png";
		public String TAB_REST;// = slug+"tab.rest.small.png";
		public String TAB_PUSH;// = slug+"tab.push.small.png";
		public String BEANSAYS_DOG_NORMAL;//= "dog.beans.says.png";
		public String BEANSAYS_DOG_NODEAL;// = slug+"nodeals.beans.png";
		public String BEANSAYS_DOG_SHADOW;// = "nodeals.beans.png";
		public String BEANSAYS_TEXT ;//= slug+"TXT.beans.says.png";
		
		public AsynchListener listener;
	
		
    @Override
        protected HashMap<String, Bitmap> doInBackground(String[]... url)
        {
 
                try {
                	HashMap<String, Bitmap> hashmap = new HashMap<String, Bitmap>();
                	String imgUri = url[0][0];
                	this.slug = url[0][1];
                	
            		
                	this.slug = ApplicationEx.getInstance().myProfile.getlastSelectedSchool().getImageSlug();
                	imgUri="http://unimiser.com/img/slugimages/android/large/"+slug+".zip";
                	
                	BEANSAYS_BTN_PUSH = slug+".beans.says.push.small.png";
            		BEANSAYS_BTN_REST =  slug+".beans.says.rest.small.png";
            		TAB_REST = slug+".tab.rest.small.png";
            		TAB_PUSH = slug+".tab.push.small.png";
            		BEANSAYS_DOG_NORMAL= ".dog.beans.says.png";
            		BEANSAYS_DOG_NODEAL = slug+".nodeals.beans.png";
            		BEANSAYS_DOG_SHADOW = ".nodeals.beans.png";
            		BEANSAYS_TEXT = slug+".TXT.beans.says.png";
            		
            		 DefaultHttpClient http = new DefaultHttpClient();
                	 HttpGet method = new HttpGet(imgUri);
                	 org.apache.http.HttpResponse res = (org.apache.http.HttpResponse) http.execute(method);
                	 len = ((org.apache.http.HttpResponse) res).getEntity().getContentLength();
                	 InputStream result = ((org.apache.http.HttpResponse) res).getEntity().getContent();
                	 
                	// Open the ZIP file
                	 // InputStream result = this.getAssets().open("unwas.zip");
                     ZipInputStream zipinputstream = new ZipInputStream(result);
                    
                    	int count = 1;ZipEntry entry;
                    	while ( (entry = zipinputstream.getNextEntry()) != null) {
                    		String zipEntryName = entry.getName();
                    		
                    	    //if (!isCorrectFilename(zipEntryName)){
                            	ByteArrayOutputStream byteOut = null;
                            	byteOut = new ByteArrayOutputStream();
                            	// replace for loop with:
                            	for (int c = zipinputstream.read(); c != -1; c = zipinputstream.read()) { 
                                  byteOut.write(c);
                                }
                            	byte[] imgByteArray =  byteOut.toByteArray();
                            	Bitmap  bm = BitmapFactory.decodeByteArray(imgByteArray,0,imgByteArray.length);
                        	    hashmap.put(zipEntryName, bm);
                        	//}else //end if
                            	  	count++;
                        }
                    return hashmap;
                 }catch (Exception e)
                {
                        e.printStackTrace();
                }
                return null;
        }

        @Override
        protected void onPostExecute(HashMap<String, Bitmap> result)
        {
                super.onPostExecute(result);
                try {
                	
                	ApplicationEx.getInstance().setHashBitmap(result,slug);
                	Intent intent = new Intent(Constant.EVENTS_UPDATESCREEN); 
                	ApplicationEx.getInstance().sendBroadcast(intent);
    				System.out.println("ZipDownLoader@OnPostExecute");
                	if (listener!=null)
                		listener.updateScreen(true);
                	} catch (Exception e) {
                    	
                    }

          	 }
          	
          		//may be remove to speed up download
        		/*private boolean isCorrectFilename(String file){
          			String filename = file.toLowerCase().trim();
          			
          			if (filename == TAB_REST.toLowerCase()){
          				return true;
          			}
          			
          			if (filename == TAB_PUSH.toLowerCase()){
          				return true;
          			}
          			
          			if (filename== BEANSAYS_BTN_REST.toLowerCase().trim()){
          				return true;
          			}
          			if (filename == BEANSAYS_BTN_PUSH.toLowerCase().trim()){
          				return true;
          			}
          			
          			if (filename == BEANSAYS_DOG_NODEAL.toLowerCase()){
          				return true;
          			}
          			
          			if (filename == BEANSAYS_DOG_NORMAL.toLowerCase()){
          				return true;
          			}
          			
          			if (filename == BEANSAYS_DOG_SHADOW.toLowerCase()){
          				return true;
          			}
          			
          			if (filename == BEANSAYS_TEXT.toLowerCase()){
          				return true;
          			}
          			
          			
          			
          			return false;
          		}*/

				
}
